<?php
	//pattern  正则表达式
	//subject  目标数据
	// preg_match(pattern, subject,[array&$matches]);表单验证等
	// preg_match_all(pattern, subject, array&$matches);
	// preg_replace(pattern, replacement, subject);非法词语过滤等
	// preg_filter(pattern, replacement, subject);
	// preg_grep(pattern, input);
	// preg_split(pattern, subject);
	// preg_quote(str);

function show($var = null){
	if(empty($var)){
		echo 'null';
	}elseif (is_array($var) || is_object($var)) {
		//array,object
		echo '<pre>';
		print_r($var);
		echo '</pre>';
	}else {
		//string,int float...
		echo $var;
	}
}


// preg_match(pattern, subject,[array&$matches]);匹配成功一次就停止
// preg_match_all(pattern, subject, array&$matches);
// 在subject中匹配pattern正则表达式，返回匹配成功的次数。匹配成功的数据存放在数组matches中，
// $pattern ='/[0-9]/';
//$subject = 'aweijojo23ij23233jior435';
/*$m1 = $m2 =array();
$t1=preg_match($pattern, $subject,$m1);
$t2=preg_match_all($pattern, $subject, $m2);
show($m1);

echo'<hr/>';
show($m2);
echo'<hr/>';
show($t1.'||'.$t2);*/




// preg_replace(pattern, replacement, subject);
// preg_filter(pattern, replacement, subject);
//在subject中匹配pattern表达式，用replacement替换.
//区别：当对数组进行更换时，preg_filter不会返回未替换的元素，preg_replace替换/未替换的都返回
/*$replacement = array('慕','课','网');
$pattern =array('/[0123]/','/[456]/','/[789]/');
$subject= array('aweij','ojo23','j2323','ior435');
$str1 = preg_replace($pattern, $replacement, $subject);
$str2 = preg_filter($pattern, $replacement, $subject);
show($str1);
echo '<hr/>';

show($str2);*/


// preg_grep(pattern, array$input);
//返回匹配成功的数据
/*$subject= array('aweij','ojo23','j2323','ior435');
$pattern ='/[0-9]/';

$arr = preg_grep($pattern, $subject);
show($arr);*/



// preg_split(pattern, subject);
//用指定的分割pattern表达式，分割subject字符串，组成一个数组
/*$pattern ='/[0-9]/';
$subject = '慕3课7网，2改变3人生！';

$arr = preg_split($pattern, $subject);
show($arr);*/


// preg_quote(str);
// 把一个字符串中的正则表达式进行转义---在正则运算符之前加 \
//   . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -   正则运算符
$str = 'qwer{asdf}[1234]';
$str = preg_quote($str);

show($str);

?>